package com.my.commonlib.util;

import android.app.Activity;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;

import com.my.commonlib.R;


public class CommonUIUtil {

    private static View mCoverView = null,mTargetView = null;
    //是否点击cover后dismiss
    private static boolean isClick = false;

    public static void addView(Object container, View view, int anim, boolean isShowCover, boolean click) {
        if (Thread.currentThread() == Looper.getMainLooper().getThread() && view.getParent() == null && view != null) {
            ViewGroup rootView = null;
            if (container instanceof Activity) {
                rootView = (ViewGroup) ((Activity) container).getWindow()
                        .getDecorView().findViewById(android.R.id.content);
            } else if (container instanceof ViewGroup) {
                rootView = (ViewGroup) container;
            } else {
                throw new IllegalArgumentException("container should be a container.");
            }

            if (rootView == null) {
                return;
            }

            if (!isNull(mCoverView)) {
                ViewGroup tempRootView = (ViewGroup) mCoverView.getParent();
                if (!isNull(mCoverView) && !isNull(tempRootView)) {
                    tempRootView.removeView(mCoverView);
                    if (!isNull(mTargetView)) {
                        tempRootView.removeView(mTargetView);
                    }
                }
            }

            mTargetView = view;
            isClick = click;

            if (isShowCover) {
                if (isNull(mCoverView)) {
                    mCoverView = LayoutInflater.from(rootView.getContext()).inflate(R.layout.view_cover, null);
                    mCoverView.setOnClickListener(mCoverViewListener);
                }
                if (isNull(mCoverView.getParent())) {
                    rootView.addView(mCoverView);
                    mCoverView.startAnimation(AnimationUtils
                            .loadAnimation(rootView.getContext(), R.anim.abc_fade_in));
                }
                mCoverView.setTag(mTargetView);
            }

            if(mTargetView.getParent() == null) {
                rootView.addView(mTargetView);
                if (anim != 0) {
                    mTargetView.startAnimation(AnimationUtils
                            .loadAnimation(rootView.getContext(), anim));
                }
            }
        }
    }
    public static void removeView(Object container, View view) {
        if (!isNull(view) && Thread.currentThread() == Looper.getMainLooper().getThread()) {
            try {
                ViewGroup rootView = null;
                if (container instanceof Activity) {
                    rootView = (ViewGroup) ((Activity) container).getWindow()
                            .getDecorView().findViewById(android.R.id.content);
                } else if (container instanceof ViewGroup) {
                    rootView = (ViewGroup) container;
                } else {
                    throw new IllegalArgumentException("container should be a container.");
                }
                rootView.removeView(view);
                rootView.removeView(mCoverView);
            } catch (Exception e) {
                /**
                 *
                 * 已经判断了是否在主线程了
                 * 可能出现 Only the original thread that created a view hierarchy can touch its views.
                 */
            }
        }
    }


    static View.OnClickListener mCoverViewListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isClick) {
                ViewGroup parent = (ViewGroup) v.getParent();
                if (!isNull(parent)) {
                    View view = (View) v.getTag(); //获取添加的view
                    removeView(parent, v);
                    removeView(parent, view);
                }
            }
        }
    };


    public static boolean isNull(Object o) {
        try {
            return null == o || "".equals(o.toString().replaceAll(" ", ""))
                    || "null".equals(o.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }



}
